﻿using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class PopMenuController : MonoBehaviour
{
    private List<string> menuMessage = new List<string>();
    private Transform fatherItem;

    public List<List<Transform>> AllMenTrans = new List<List<Transform>>();
    public InputField SearchInput;
    public GameObject MenuItemPrefab;
    public Transform ItemsGrid;
    
    // Use this for initialization
	void Start ()
	{
	    this.menuMessage = this.GetComponent<ReadMenuInfo>().MenuMessage;
        
        this.FlashMenu();

        this.SearchInput.onEndEdit.AddListener(this.SearchItem);

	}

    /// <summary>
    /// 菜单搜素
    /// </summary>
    /// <param name="arg0"></param>
    private void SearchItem(string arg0)
    {
        List<Transform> searchedItem = new List<Transform>();
        if (arg0 == "")
            this.FlashMenu();
        else
        {
            foreach (var trans in this.AllMenTrans)
            {
                foreach (var trans1 in trans)
                {
                    if (Contains(trans1.name, arg0, StringComparison.CurrentCultureIgnoreCase))
                    {
                        trans1.gameObject.SetActive(true);
                        searchedItem.Add(trans1);
                    }
                    else
                        trans1.gameObject.SetActive(false);
                }
            }
        }

        if (searchedItem.Count > 0)
        {
            foreach (var searchItem in searchedItem)
            {
                this.ShowAllFather(searchItem);
            }
        }

    }

    /// <summary>
    /// 忽略大小写的判断字符串是否包含制定字符串
    /// </summary>
    /// <param name="source"></param>
    /// <param name="value"></param>
    /// <param name="comparisonType"></param>
    /// <returns></returns>
    public static bool Contains(string source, string value, StringComparison comparisonType)
    {
        return (source.IndexOf(value, comparisonType) >= 0);
    }

    private void ShowAllFather(Transform item)
    {
        if (item.GetComponent<MenuItem>())
        {
            MenuItem menuItem = item.GetComponent<MenuItem>();
            if (menuItem.FatherItem.GetComponent<MenuItem>())
            {
                menuItem.FatherItem.gameObject.SetActive(true);
                this.ShowAllFather(menuItem.FatherItem);
            }
        }
    }

    /// <summary>
    /// 刷新菜单
    /// </summary>
    private void FlashMenu()
    {
        for (int i = 0; i < this.ItemsGrid.childCount; i++)
        {
            GameObject.Destroy(this.ItemsGrid.GetChild(i).gameObject);
        }

        this.AllMenTrans.Clear();

        foreach (var msg in this.menuMessage)
        {
            string[] temp = msg.Split('/');
            this.fatherItem = this.ItemsGrid;
            Transform tempItem = this.ItemsGrid;
            for (int i = 0; i < temp.Length - 1; i++)
            {
                if (this.JudgeHadItem(i, temp[i], temp[0]) == null)
                {
                    tempItem = GameObject.Instantiate(this.MenuItemPrefab, this.ItemsGrid).transform;
                    tempItem.GetComponent<MenuItem>().name = temp[i];
                    tempItem.GetComponent<MenuItem>().FirstLevName = temp[0];
                    tempItem.GetComponent<MenuItem>().NameText.text = temp[i];
                    tempItem.GetComponent<MenuItem>().ContentTrans.position += new Vector3(25 * i, 0);
                    if (i > 0)
                        this.fatherItem.GetComponent<MenuItem>().ChildItemTrans.Add(tempItem);
                    if (i == temp.Length - 2)
                        tempItem.GetComponent<MenuItem>().SelfToggle.gameObject.SetActive(false);
                    tempItem.GetComponent<MenuItem>().FatherItem = this.fatherItem;
                    this.fatherItem = tempItem;
                    this.AllMenTrans[i].Add(tempItem);
                }
                else
                {
                    this.fatherItem = this.JudgeHadItem(i, temp[i], temp[0]);
                }
            }
        }

        this.HideAllChild();
    }

    /// <summary>
    /// 判断菜单库内是否已经存在要加入的菜单
    /// </summary>
    /// <param name="lve"></param>
    /// <param name="itemName"></param>
    /// <param name="firstLevName"></param>
    /// <returns></returns>
    private Transform JudgeHadItem(int lve, string itemName, string firstLevName)
    {
        if (lve >= this.AllMenTrans.Count)
        {
            List<Transform> tempMenus = new List<Transform>();
            this.AllMenTrans.Add(tempMenus);
        }
        foreach (var temp in this.AllMenTrans[lve])
        {
            if (temp.name == itemName && temp.GetComponent<MenuItem>().FirstLevName == firstLevName)
                return temp;
        }
        return null;
    }

    private void HideAllChild()
    {
        for (int i = 1; i < this.AllMenTrans.Count; i++)
        {
            foreach (var childTrans in this.AllMenTrans[i])
            {
                childTrans.gameObject.SetActive(false);
            }
        }
    }
}
